$(document).ready(function () {
    appBosch.iRow = 1;
    var dataSource = new kendo.data.DataSource({
        data: appBosch.data,
        schema: {
            model: {
                fields: {
                    certifications: {type: 'number'},
                    trainings: {type: 'number'},
                    assign_date: {type: 'date'},
                    days: {type: 'number'}
                }
            }
        },
        group: {
            field: 'line', aggregates: [
                {field: 'certifications', aggregate: 'sum'},
                {field: 'trainings', aggregate: 'sum'},
                {field: 'assign_date', aggregate: 'min'},
                {field: 'assign_date', aggregate: 'max'},
                {field: 'days', aggregate: 'min'},
                {field: 'days', aggregate: 'max'}
            ]
        }
    });

    $("#grid").kendoGrid({
        toolbar: [
            {
                name: "excel",
                text: "Exportar a Excel"
            },
            {
                name: "pdf",
                text: "Exportar a PDF"
            }
        ],
        excel: {
            allPages: true,
            fileName: "Asociados por línea.xlsx",
            filterable: true
        },
        pdf: {
            allPages: true,
            fileName: "Asociados por línea.pdf"
        },
        resizable: true,
        columnMenu: true,
        scrollable: true,
        filterable: {multi: true},
        selectable: "multiple cell",
        navigatable: true,
        sortable: true,
        allowCopy: true,
        dataSource: dataSource,
        dataBound: function () {
            appBosch.iRow = 1;
        },
        columns: [
            {title: '#', template: '#=appBosch.iRow++#', width: 40},
            {field: "nomina", title: "Nomina", filterable: {multi: false}},
            {field: "name", title: "Asociado", hidden: true, filterable: {multi: false}},
            {field: "curp", title: "CURP", hidden: true, filterable: {multi: false}},
            {field: "moe", title: "MOE", hidden: true},
            {field: "area", title: "Área", hidden: true},
            {field: "line", title: "Línea"},
            {
                field: "level_excel_name",
                title: "Nivel excel",
                template: '<i title="#=level_excel_name#" class="fa-2x fa #=level_excel_icon#" style="color: #=level_excel_colour#"></i>'
            },
            {
                field: "level_migration_name",
                title: "Nivel migración",
                template: '<i title="#=level_migration_name#" class="fa-2x fa #=level_migration_icon#" style="color: #=level_migration_colour#"></i>'
            },
            {
                field: "level_name",
                hidden: true,
                title: "Nivel actual",
                template: '<i title="#=level_name#" class="fa-2x fa #=level_icon#" style="color: #=level_colour#"></i>'
            },
            {
                field: "certifications",
                title: "Certificaciones",
                template: "#=kendo.toString(certifications, 'n0')#",
                groupFooterTemplate: "#=sum#",
                filterable: {multi: false}
            },
            {
                field: "trainings",
                title: "Certificaciones en entrenamiento",
                template: "#=kendo.toString(trainings, 'n0')#",
                groupFooterTemplate: "#=sum#",
                filterable: {multi: false}
            },
            {
                field: "assign_date",
                title: "Fecha de ingreso",
                template: "<span title='#=kendo.toString(new Date(assign_date), 'D')#'>#=kendo.toString(new Date(assign_date), 'd')#</span>",
                groupFooterTemplate: "Min: #=kendo.toString(min, 'd')# Max: #=kendo.toString(max, 'd')#",
                filterable: {multi: false}
            },
            {
                field: "days",
                title: "Antigüedad",
                template: "#=kendo.toString(days, 'n0')# días",
                groupFooterTemplate: "Min: #=kendo.toString(min, 'n0')# días Max: #=kendo.toString(max, 'n0')# días",
                filterable: {multi: false}
            }
        ]
    });
});